#!/bin/bash
#version--20190620
####ljt
#user=`users`
user=`users |cut -d' ' -f1`

if [ -e "/dev/ttyS0" ]; then
    s_S_L="1"
else
    s_S_L="0"
fi

if [ -e "/dev/ttyM0" ]; then
    s_M_L="1"
else
    s_M_L="0"
fi

if [ -e "/dev/ttyUSB0" ]; then
    s_USB_L="1"
else
    s_USB_L="0"
fi


if [ "${s_S_L}" != "0" ]; then
	sgroup=`ls -l /dev/ttyS*|awk -F" " 'NR==1{print $4}'`
	sname="/dev/ttyS*"
elif [ "${s_M_L}" != "0" ]; then
	sgroup=`ls -l /dev/ttyM*|awk -F" " 'NR==1{print $4}'`
	sname="/dev/ttyM*"
elif [ "${s_USB_L}" != "0" ]; then
	sgroup=`ls -l /dev/ttyUSB*|awk -F" " 'NR==1{print $4}'`
	sname="/dev/ttyUSB*"
else
	echo "no find serial or on support this serial"
fi

echo "Now add user:${user} to groups:${sgroup}"
usermod -aG ${sgroup} ${user}

cmd=`groups ${user}|grep ${sgroup} |wc -l`
if [ "${cmd}" = "1" ]; then
    echo "${sname} ok!"
else
	echo "failed"
fi
